var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    canvasHidden:true,
    structure_name:'',
    isbumen:0  //0关闭,1添加部门
  },
  // 添加部门
  add_bumen:function(){
    this.setData({
      isbumen:1,
      part_name:''
    });
  },
  // 关闭部门
  cancel_add_bumen:function(){
    this.setData({
      isbumen:0
    });
  },
  // 输入部门名称
  bumen_input:function(e){
    this.setData({
      part_name:e.detail.value
    });
  },
  // 确定添加部门
  conf_add_bumen:function(){
    var that = this;
    if(that.data.part_name==''){
      wx.showLoading({
        title:'输入为空',
        duration:500
      });
      return;
    };
    app.AppHttp('api/Client/addDepartMent', {
      client_id:app.globalData.loginInfo.id,
      part_name:that.data.part_name,
      depart_id:0
    }, function (res) {
      console.log(JSON.stringify(res.data));
      that.setData({
        isbumen:0
      });
      that.loadlist();
    });   
  },
  // 查看用户
  itemClick: function (event) {
    console.log(event);
    var index = event.currentTarget.id;
    var yg = this.data.person_list[index];
    console.log(yg.nickname);
    if (yg.is_validate == 1){
      wx.navigateTo({
        url: 'yuangongshezhi/yuangongshezhi?id='+yg.id+'&nick_name='+yg.nickname
      })     
    }
  },
  // 去部门
  todepart:function(e){
    var index=e.currentTarget.dataset.index;
    var id=this.data.children_part[index].id;
    var part_name=this.data.children_part[index].part_name;
    wx.navigateTo({
      url:'/pages/wo/jigouquanxian/guanlichufang/yuangongguanli/depart/depart?id='+id+'&part_name='+part_name+'&layer_list='+JSON.stringify(['全员'])+'&lay_num='+1
    });
  },
  // 批量移动成员
  batch_move:function(){
    wx.setStorageSync('children_part_ids',[]) //存 
    wx.setStorageSync('person_ids',[]) //存 
    wx.setStorageSync('children_obj',{}) //存 
    wx.setStorageSync('person_obj',{}) //存 
    wx.setStorageSync('all_index',[]) //存 
    wx.navigateTo({
      url:"check_depart/check_depart?type="+2+'&check_name='+'移动成员'+'&lay_num='+0+'&id='+0+'&page_num='+0
    });
  },
  // 接受员工
  jieshouClick:function(event){
    var index = event.currentTarget.id;
    var yg = this.data.person_list[index];
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定接受？',
      success: function (sm) {
        if (sm.confirm) {
          wx.request({
            url: app.globalData.url + 'api/Client/examineEmployee',
            method: "POST",
            data: {
              openid: app.globalData.openid,
              id: yg.id
            },
            success: function (res) {
              console.log(JSON.stringify(res.data));
              yg.is_validate = 1;
              that.setData({
                ["person_list[" + index + "]"]: yg
              });
            }
          });
        } else if (sm.cancel) {
          console.log('用户点击取消')
        }
      }
    });   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      width: wx.getSystemInfoSync().windowWidth,
      client_logo:app.globalData.loginInfo.logo!=0?app.globalData.loginInfo.logo:'http://www.xiaobangcai.com/upload/smallprogram/client_def.png',
      gname:app.globalData.loginInfo.client_abbre
    });
  },
  // 页面刷新
  loadlist:function(){
    var that = this;
    app.AppHttp('api/Client/structureManage', {
      id:app.globalData.loginInfo.id
    }, function (res) {
      console.log(JSON.stringify(res.data));
      that.setData({
        person_list: res.data.list,
        children_part:res.data.department
      }); 
    });  
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.hideShareMenu();//隐藏转发分享按钮
    this.setData({
      canvasHidden:true
    });
    this.loadlist();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.loadlist();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this;
    that.setData({
      canvasHidden: false
    });
    return {
      title: ' '+that.data.gname+'-邀请您加入',
      path: '/pages/wo/tuijian/tuijian?ShareOpenid=' + app.globalData.openid + '&type=' + 'invite'+'&part_id='+0
    }
  }
})